<?php

namespace App\Http\Requests;

use App\Models\Cashflow;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class CashflowRequest extends FormRequest
{
    /**
     * 用户是否有权限请求
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 精确到控制器方法的验证
     *
     * @return array
     */
    public function rules()
    {
        $actionMethod = request()->route()->getActionMethod();
        $ruleMethod = $actionMethod . 'Rule';
        if (method_exists($this, $ruleMethod)) {
            return $this->{$ruleMethod}();
        }

        return [];
    }

    private function storeRule()
    {
        $rule = [
            'user_id' => 'integer',
            'type' => [
                'required',
                'integer',
                Rule::in([Cashflow::TYPE_IN, Cashflow::TYPE_OUT])
            ],
            'category_id' => 'integer',
            'amount' => 'numeric',
            'year' => 'integer',
            'month' => 'integer',
            'day' => 'integer',
            'date' => 'date',
            'image' => 'string',
            'remark' => 'string',
        ];

        return $rule;
    }

    public function appStoreRule()
    {
        $rule = [
            'type' => 'required|integer',
            'category_id' => 'required|integer',
            'cashbook_id' => 'required|integer',
            'amount' => 'required|numeric',
            'date' => 'required|date',
            'image' => 'nullable|string',
            'remark' => 'nullable|string',
        ];

        return $rule;
    }

    public function appUpdateRule()
    {
        $rule = [
            'id' => 'required|integer',
            'type' => [
                'required',
                'integer',
                Rule::in([Cashflow::TYPE_IN, Cashflow::TYPE_OUT])
            ],
            'category_id' => 'required|integer',
            'amount' => 'required|numeric',
            'date' => 'required|date',
            'image' => 'nullable|string',
            'remark' => 'nullable|string',
        ];

        return $rule;
    }

    public function importRule()
    {
        $rule = [
            'file_path' => 'required|string',
            'user_id' => 'required|integer',
        ];

        return $rule;
    }

    public function appListRule()
    {
        $rule = [
            'cashbook_id' => 'required|integer',
            'year' => 'required|integer',
            'month' => 'required|integer|min:1|max:12'
        ];

        return $rule;
    }

    private function updateRule()
    {
        $rule = [
            'user_id' => 'integer',
            'type' => [
                'required',
                'integer',
                Rule::in([Cashflow::TYPE_IN, Cashflow::TYPE_OUT])
            ],
            'category_id' => 'integer',
            'amount' => 'numeric',
            'year' => 'integer',
            'month' => 'integer',
            'day' => 'integer',
            'date' => 'date',
            'image' => 'string',
            'remark' => 'string',

        ];

        return $rule;
    }

    public function attributes()
    {
        return [
            'id' => '',
            'user_id' => '关联用户,创建记录的用户',
            'type' => '账单类型 收入 支出',
            'category_id' => '收支分类',
            'cashbook_id' => '账本',
            'amount' => '金额数量',
            'year' => '年 冗余统计',
            'month' => '月 冗余统计',
            'day' => '日 冗余统计',
            'date' => '日期',
            'image' => '图片',
            'remark' => '备注'
        ];
    }

    // 常用验证方法 https://learnku.com/docs/laravel/5.8/validation/3899#rule-accepted
}
